package com.xkl.springdoc.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * OpenApi信息
 *
 * @version: 1.0.0-SNAPSHOT
 * @author: 码农小卡拉
 * @createTime: 2025-09-17 10:49
 **/
@Configuration
public class SpringDocAutoConfiguration {

    // 用户模块文档分组
    @Bean
    public GroupedOpenApi userApi() {
        return GroupedOpenApi.builder()
                .group("用户模块") // 分组名称
                .pathsToMatch("/api/user/**", "/api/role/**") // 该分组包含的接口路径
                .build();
    }

    // 订单模块文档分组
    @Bean
    public GroupedOpenApi saleListApi() {
        return GroupedOpenApi.builder()
                .group("订单模块") // 分组名称
                .pathsToMatch("/api/saleList/**") // 该分组包含的接口路径
                .build();
    }

    @Bean
    public OpenAPI springShopOpenAPI() {
        OpenAPI openAPI = new OpenAPI();
        openAPI.info(new Info()
                .title("SpringShop API")
                .description("Spring Doc 示例应用")
                .version("v0.0.1")
                .contact(new Contact().name("码农小卡拉").url("https://mp.weixin.qq.com/s/ty576TjLspSF5ygkjfWnwA"))
                .license(new License().name("Apache 2.0").url("http://springdoc.org")))
        .externalDocs(new io.swagger.v3.oas.models.ExternalDocumentation()
                .description("CSDN 博客")
                .url("https://blog.csdn.net/spb229443329"));

        // 定义 JWT 认证方案
        SecurityScheme jwtScheme = new SecurityScheme()
                .type(SecurityScheme.Type.HTTP)
                .scheme("bearer")
                .bearerFormat("JWT")
                .in(SecurityScheme.In.HEADER)
                .name("Authorization"); // Token 放在 Header 的 Authorization 字段
        // 全局启用 JWT 认证
        SecurityRequirement securityRequirement = new SecurityRequirement().addList("JWT");
        openAPI.addSecurityItem(securityRequirement).components(new Components().addSecuritySchemes("JWT", jwtScheme));
        return openAPI;
    }
}
